package com.example.backend.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.backend.domain.entity.RestBean;
import com.example.backend.domain.po.Reviews;
import com.example.backend.service.IReviewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dyy
 * @since 2025-05-27
 */
@RestController
@RequestMapping("/reviews")
public class ReviewsController {
    @Autowired
    private IReviewsService reviewsService;

    // 根据 ID 查询单个评价
    @GetMapping("/{id}")
    public RestBean<Reviews> getReviewById(@PathVariable Integer id) {
        Reviews review = reviewsService.getById(id);
        return RestBean.success(review);
    }

    // 根据多个 ID 查询评价
    @GetMapping("/batch")
    public RestBean<List<Reviews>> getReviewsByIds(@RequestParam List<Integer> ids) {
        List<Reviews> reviews = reviewsService.listByIds(ids);
        return RestBean.success(reviews);
    }

    // 根据条件查询评价
    @GetMapping("/search")
    public RestBean<List<Reviews>> searchReviews(
            @RequestParam(required = false) Integer appointmentId,
            @RequestParam(required = false) Integer rating) {
        LambdaQueryWrapper<Reviews> queryWrapper = new LambdaQueryWrapper<>();
        if (appointmentId != null) {
            queryWrapper.eq(Reviews::getAppointmentId, appointmentId);
        }
        if (rating != null) {
            queryWrapper.eq(Reviews::getRating, rating);
        }
        List<Reviews> reviews = reviewsService.list(queryWrapper);
        return RestBean.success(reviews);
    }

    // 分页查询评价
    @GetMapping("/page")
    public RestBean<IPage<Reviews>> getReviewsByPage(
            @RequestParam Integer current,
            @RequestParam Integer size,
            @RequestParam(required = false) Integer appointmentId,
            @RequestParam(required = false) Integer rating) {
        Page<Reviews> page = new Page<>(current, size);
        LambdaQueryWrapper<Reviews> queryWrapper = new LambdaQueryWrapper<>();
        if (appointmentId != null) {
            queryWrapper.eq(Reviews::getAppointmentId, appointmentId);
        }
        if (rating != null) {
            queryWrapper.eq(Reviews::getRating, rating);
        }
        IPage<Reviews> reviewPage = reviewsService.page(page, queryWrapper);
        return RestBean.success(reviewPage);
    }

    // 新增评价
    @PostMapping
    public RestBean<Void> addReview(@RequestBody Reviews review) {
        boolean save = reviewsService.save(review);
        return RestBean.success();
    }

    // 更新评价信息
    @PutMapping("/{id}")
    public RestBean<Void> updateReview(
            @PathVariable Integer id,
            @RequestBody Reviews review) {
        review.setId(id);
        boolean update = reviewsService.updateById(review);
        return RestBean.success();
    }

    // 删除评价
    @DeleteMapping("/{id}")
    public RestBean<Void> deleteReview(@PathVariable Integer id) {
        boolean remove = reviewsService.removeById(id);
        return RestBean.success();
    }
}
